package comm

import (
	"duoke/utils/response"
	"duoke/websocket"
	"github.com/gin-gonic/gin"
	"github.com/gogf/gf/v2/encoding/gjson"
	"github.com/gogf/gf/v2/util/gconv"
)

var WsMsg = new(wsMsgCtl)

type wsMsgCtl struct{}

func (ctl *wsMsgCtl) Send(c *gin.Context) {
	uid, _ := c.GetPostForm("uid")
	event, _ := c.GetPostForm("event")
	data, _ := c.GetPostForm("data")
	if uid == "" {
		response.Error(c, "uid缺失", nil)
		return
	}
	if event == "" {
		response.Error(c, "Event缺失", nil)
		return
	}
	if data == "" {
		response.Error(c, "Data缺失", nil)
		return
	}
	// 发送消息
	websocket.SendToUser(gconv.Uint64(uid), &websocket.WResponse{
		Event: event,
		Data:  gjson.New(data).Map(),
	})
	response.Success(c, "success", nil)
}
